<template>
    <view>
        <view
            v-show="dragInfo.drag_btn_switch"
            id="_drag_button"
            class="drag"
            :style="'left: ' + left + 'px; top:' + top + 'px;' + dragStyle"
            @touchstart="touchstart"
            @touchmove.stop.prevent="touchmove"
            @touchend="touchend"
            @click.stop.prevent="click"
            :class="{ transition: isDock && !isMove }"
        >
            <text>{{ text }}</text>
        </view>
    </view>
</template>

<script>
export default {
    name: 'drag-button',
    props: {
        isDock: {
            type: Boolean,
            default: false
        },
        dragInfo: {
            type: [Object, Array],
            default: function() {
                return;
            }
        },
        existTabBar: {
            type: Boolean,
            default: false
        },
        dragStyle: {
            type: String,
            default:
                'background-color: rgba(0, 0, 0, 0.5);box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);border-radius: 50%;'
        }
    },
    data() {
        return {
            top: 0,
            left: 0,
            width: 0,
            height: 0,
            offsetWidth: 0,
            offsetHeight: 0,
            windowWidth: 0,
            windowHeight: 0,
            isMove: false,
            edge: 10,
            text: '',
            timer: null,
            isClick: false
        };
    },
    watch: {
        dragStyle(nval) {
            if (nval == '') {
                return 'background-color: rgba(0, 0, 0, 0.5);box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);border-radius: 50%;';
            }
        }
    },
    mounted() {
        const sys = uni.getSystemInfoSync();
        this.windowWidth = sys.windowWidth;
        this.windowHeight = sys.windowHeight;
        // #ifdef APP-PLUS
        this.existTabBar && (this.windowHeight -= 50);
        // #endif
        if (sys.windowTop) {
            this.windowHeight += sys.windowTop;
        }
        const query = uni.createSelectorQuery().in(this);
        query
            .select('#_drag_button')
            .boundingClientRect(data => {
                this.width = data.width;
                this.height = data.height;
                this.offsetWidth = data.width / 2;
                this.offsetHeight = data.height / 2;
                this.left = this.windowWidth - this.width - this.edge;
                this.top = (this.windowHeight / 3) * 2;
            })
            .exec();
        this.hideBtn();
    },
    methods: {
        click() {
            clearTimeout(this.timer);
            this.isClick = true;
            this.$emit('btnClick');
        },
        touchstart(e) {
            clearTimeout(this.timer);
            this.isShow = true;
            this.$emit('btnTouchstart');
        },
        touchmove(e) {
            clearTimeout(this.timer);
            this.isShow = true;
            // 单指触摸
            if (e.touches.length !== 1) {
                return false;
            }
            this.isMove = true;
            this.left = e.touches[0].clientX - this.offsetWidth;
            let clientY = e.touches[0].clientY - this.offsetHeight;
            // #ifdef H5
            clientY += this.height;
            // #endif
            let edgeBottom = this.windowHeight - this.height - this.edge;
            // 上下触及边界
            if (clientY < this.edge) {
                this.top = this.edge;
            } else if (clientY > edgeBottom) {
                this.top = edgeBottom;
            } else {
                this.top = clientY;
            }
            // 左右触及边界
            if (this.left < this.edge) {
                this.left = this.edge;
            } else if (this.left > this.windowWidth - this.width - this.edge) {
                this.left = this.windowWidth - this.width - this.edge;
            }
        },
        touchend(e) {
            if (this.isDock) {
                let edgeRigth = this.windowWidth - this.width - this.edge;
                if (this.left < this.windowWidth / 2 - this.offsetWidth) {
                    this.left = this.edge;
                } else {
                    this.left = edgeRigth;
                }
            }
            this.isMove = false;
            this.$emit('btnTouchend');
            if (this.isClick) {
                clearTimeout(this.timer);
                return;
            }
            this.hideBtn();
        },
        hideBtn() {
            // let _this = this;
            // clearTimeout(_this.timer);
            // setTimeout(v => {
            //     _this.isClick = false;
            //     _this.$emit('hide');
            //     // 右边
            //     if (this.left > this.windowWidth / 2 - this.offsetWidth) {
            //         _this.left =
            //             _this.left + (_this.width + _this.edge * 2) / 2;
            //     } else {
            //         _this.left = 0 - _this.width / 2;
            //     }
            // }, 3000);
        },
        navTo() {
            switch (this.dragInfo.drag_btn_type) {
                case '1':
                    // #ifdef MP-WEIXIN
                    uni.navigateTo({
                        url: this.dragInfo.drag_btn_page,
                        complete(e) {
                            console.log(e);
                        }
                    });
                    // #endif
                    break;
                case '2':
                    // #ifdef MP-WEIXIN
                    // 是否是短链接
                    if (
                        this.dragInfo.drag_btn_page.indexOf('#小程序://') == -1
                    ) {
                        uni.navigateToMiniProgram({
                            appId: this.dragInfo.drag_btn_appid,
                            path: this.dragInfo.drag_btn_page
                        });
                    } else {
                        uni.navigateToMiniProgram({
                            shortLink: this.dragInfo.drag_btn_page
                        });
                    }
                    // #endif
                    break;
                case '3':
                    // #ifdef APP-PLUS
                    uni.navigateTo({
                        url:
                            '/pages/webview/about?url=' +
                            encodeURIComponent(this.dragInfo.drag_btn_page)
                    });
                    // #endif
                    break;
                case '4':
                    let _this = this,
                        res = [],
                        image =
                            this.$config.apiImageUrl +
                            uni.getStorageSync('sys_config').kefu_qrcode;
                    res.push(image);
                    // 预览图片
                    uni.previewImage({
                        urls: res,
                        longPressActions: {
                            itemList: ['保存图片']
                        }
                    });
            }
        }
    }
};
</script>

<style lang="scss">
.drag {
    display: flex;
    justify-content: center;
    align-items: center;
    color: $uni-text-color-inverse;
    width: 80upx;
    height: 80upx;
    font-size: $uni-font-size-sm;
    position: fixed;
    z-index: 999999;

    &.transition {
        transition: left 0.3s ease, top 0.3s ease;
    }
}
</style>
